package cn.jzvd.demo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ArrayUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.jzvd.Jzvd;
import cn.jzvd.demo.utils.FileComparator;
import io.objectbox.Box;

/**
 * Created by Nathen on 16/7/22.
 */
public class ActivityMain extends AppCompatActivity {

    Jzvd myJzvdStd;
    private Button tv_add_mark;
    private Button tv_del_mark;
    private Box<VideoTag> accountPSDBeanBox;

    //记录第几次标记
    private int markNumber = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myJzvdStd = findViewById(R.id.jz_video);
        tv_add_mark = findViewById(R.id.tv_add_mark);
        tv_del_mark = findViewById(R.id.tv_del_mark);
        //本地文件转URL
//        URL url = null;
//        try {
//            url = new File("d:/temp/a.txt").toURI().toURL();
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//        myJzvdStd.setUp(url.getPath()
//                , "饺子快长大");

//        VideoTag big_buck_bunny = VideoTag.inquiry("big_buck_bunny");


        VideoTag videoTag = VideoTag.inquiry("big_buck_bunny");

        ArrayList arrayList = new ArrayList();

        if (videoTag != null) {

            long totaltime = videoTag.getTotaltime();

            long tag1 = videoTag.getTag1();
            long tag2 = videoTag.getTag2();
            long tag3 = videoTag.getTag3();
            long tag4 = videoTag.getTag4();
            long tag5 = videoTag.getTag5();
            long tag6 = videoTag.getTag6();
            long tag7 = videoTag.getTag7();
            long tag8 = videoTag.getTag8();


            if (0 != totaltime) {

                //tag单位是毫秒 求得百分比再放大1000倍
                arrayList.add(tag1 * 1000 / totaltime);
                arrayList.add(tag2 * 1000 / totaltime);
                arrayList.add(tag3 * 1000 / totaltime);
                arrayList.add(tag4 * 1000 / totaltime);
                arrayList.add(tag5 * 1000 / totaltime);
                arrayList.add(tag6 * 1000 / totaltime);
                arrayList.add(tag7 * 1000 / totaltime);
                arrayList.add(tag8 * 1000 / totaltime);
            }


            Log.e("TAG", "获取保存的千分比 集合: ==========" + Arrays.toString(arrayList.toArray()));
        }


        myJzvdStd.setData(arrayList);


        myJzvdStd.setUp("https://media.w3.org/2010/05/sintel/trailer.mp4"
                , "饺子快长大", Jzvd.SCREEN_NORMAL);

        tv_del_mark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VideoTag.delete("big_buck_bunny");
            }
        });

        tv_add_mark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取视频总时长
                long videoDurationTotal = myJzvdStd.getLong();
                //获取当前播放时间毫秒
                long currentPlayingTime = myJzvdStd.getCurrentPlayingTime();

                Log.e("TAG", "onClick: ===========videoDurationTotal=" + videoDurationTotal );
                Log.e("TAG", "onClick: ===========currentPlayingTime=" + currentPlayingTime );

                if (0 == videoDurationTotal || 0 == currentPlayingTime) {
                    ToastUtils.showShort("视频还未播放，无法添加标记");
                    return;
                }

                if (markNumber >= 8) {
                    return;
                }

//                accountPSDBeanBox = ApplicationDemo.getBoxStore().boxFor(VideoTag.class);
//                accountList = accountPSDBeanBox.query().build().find();


                VideoTag.add("big_buck_bunny", markNumber, currentPlayingTime, videoDurationTotal);

                markNumber++;

                tv_add_mark.setText("标记:" + markNumber);


            }
        });

//        getAssertVideoToLocalPath();
//        Glide.with(this).load("http://jzvd-pic.nathen.cn/jzvd-pic/1bb2ebbe-140d-4e2e-abd2-9e7e564f71ac.png").into(myJzvdStd.posterImageView);

    }

//    private List<VideoTag> accountList;

    public void getAssertVideoToLocalPath() {
//

        String mPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/ShunFengEr/movie" + "/2021_05_25_10_44_42.mp4";
        String mPath0 = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/ShunFengEr/picture";
        List<File> mListFiles = getFileList(mPath, mPath0);

        for (int i = 0; i < mListFiles.size(); i++) {

            refresh(mListFiles.get(i).getAbsolutePath());
        }

        Log.e("TAG", "getAssertVideoToLocalPath: ==========" + mPath);

        Log.e("TAG", "getAssertVideoToLocalPath: ==========" + mListFiles.size());

        myJzvdStd.setUp(mPath, "饺子524514不信");

    }

    private void refresh(String filePath) {
        Uri localUri = Uri.fromFile(new File(filePath));
        Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
        sendBroadcast(localIntent);
    }

    /*
     * 根据地址获取当前地址下的所有目录和文件，并且排序
     * @param path
     * @return List<File>
     */
    private List<File> getFileList(String path, String path0) {
        return getFileListByDirPath(path, path0, null);
    }

    public static List<File> getFileListByDirPath(String path, String path0, FileFilter filter) {
        File directory = new File(path);
        File directory0 = new File(path0);
        File[] files = directory.listFiles(filter);
        File[] files0 = directory0.listFiles(filter);

        File[] add = ArrayUtils.add(files, files0);

        if (add == null) {
            return new ArrayList<>();
        }

        List<File> result = Arrays.asList(add);
        Collections.sort(result, new FileComparator());
        return result;
    }


    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

}
